package com.googlecode.mailnet.emf.netmodel.diagram.edit.parts;

import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles;
import org.eclipse.gmf.runtime.notation.View;

import com.googlecode.mailnet.emf.netmodel.NetworkConfigurationModel;
import com.googlecode.mailnet.emf.netmodel.diagram.edit.policies.NetworkConfigurationModelCanonicalEditPolicy;
import com.googlecode.mailnet.emf.netmodel.diagram.edit.policies.NetworkConfigurationModelItemSemanticEditPolicy;
import com.googlecode.mailnet.emf.netmodel.simulation.DecorationUtils;
import com.googlecode.mailnet.emf.netmodel.visualization.ModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.bottleneck.BottleneckModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.failure.FailureModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.failureprobability.FailureProbabilityModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.highrisk.HighRiskModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.importance.ImportanceModelDecorator;
import com.googlecode.mailnet.emf.netmodel.visualization.impstep.LinkImportanceModelDecorator;

/**
 * @generated
 */
public class NetworkConfigurationModelEditPart extends DiagramEditPart {

	/**
	 * @generated
	 */
	public final static String MODEL_ID = "Netmodel"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static final int VISUAL_ID = 79;

	/**
	 * @generated
	 */
	public NetworkConfigurationModelEditPart(View view) {
		super(view);
	}

	/**
	 * @generated
	 */
	protected void createDefaultEditPolicies() {
		super.createDefaultEditPolicies();
		installEditPolicy(EditPolicyRoles.SEMANTIC_ROLE,
				new NetworkConfigurationModelItemSemanticEditPolicy());
		installEditPolicy(EditPolicyRoles.CANONICAL_ROLE,
				new NetworkConfigurationModelCanonicalEditPolicy());
		// removeEditPolicy(org.eclipse.gmf.runtime.diagram.ui.editpolicies.EditPolicyRoles.POPUPBAR_ROLE);
	}

	private ModelDecorator decorator = null;
	
	public void setDecorator(ModelDecorator decorator) {
		this.decorator = decorator;
		NetworkConfigurationModel model = (NetworkConfigurationModel) resolveSemanticElement();
		if (model.getSimulationContext() != null) {
			this.decorator.apply(this, model.getSimulationContext());
			this.getFigure().repaint();
		}
	}
	
	public ModelDecorator getDecorator() {
		return decorator;
	}
	
	public void onDecoratorUpdated() {	
		NetworkConfigurationModel model = (NetworkConfigurationModel) resolveSemanticElement();
		System.out.println("Decorator updated: " + (model == null ? "NULL" : model.getDecoratorId()));
		if (decorator != null) decorator.detach();
		try {
			if (DecorationUtils.DECORATOR_FAILURES.equals(model.getDecoratorId())) {
				setDecorator(new FailureModelDecorator());
			} else if (DecorationUtils.DECORATOR_FAILURE_PROBABILITY.equals(model.getDecoratorId())) {
				setDecorator(new FailureProbabilityModelDecorator());
			} else if (DecorationUtils.DECORATOR_IMPORTANCE_PER_STEP.equals(model.getDecoratorId())) {
				setDecorator(new LinkImportanceModelDecorator());
			} else if (DecorationUtils.DECORATOR_AVERAGE_IMPORTANCE.equals(model.getDecoratorId())) {
				setDecorator(new ImportanceModelDecorator());
			} else if (DecorationUtils.DECORATOR_BOTTLE_NECK.equals(model.getDecoratorId())) {
				setDecorator(new BottleneckModelDecorator());
			} else if (DecorationUtils.DECORATOR_HIGH_RISK.equals(model.getDecoratorId())) {
				setDecorator(new HighRiskModelDecorator());
			} else {
				ModelDecorator.detach(this);
			}
		} catch (Exception e) {
			System.out.println(e.toString());
		}
		this.getFigure().repaint();
	}
}
